#ifndef SYSTEM_MANAGER_H
#define SYSTEM_MANAGER_H

#include "common.h"
#include "database/db_manager.h"  // 新增数据库依赖
#include "event_loop.h"
#include "data_pool.h"
#include "config_manager.h"
#include "timer_manager.h"
#include "global_config.h"
#include "http/http_server.h"



typedef struct {
    event_loop_t *event_loop;
    data_pool_t *data_pool;
    config_manager_t *config;
    timer_manager_t *timer;
    db_manager_t *db_mgr;         // 新增数据库管理器
    global_config_t global_config;  // 新增全局配置
    http_server_t http_server;      // 新增HTTP服务器
    bool initialized;
    bool running;
} system_manager_t;

// 新增API
int system_manager_update_device_api_config(system_manager_t *manager, const char *ip, int port);
int system_manager_update_http_server_config(system_manager_t *manager, const char *ip, int port);
int system_manager_restart_http_server(system_manager_t *manager);

// 系统管理器API
int system_manager_init(system_manager_t *manager);
int system_manager_start(system_manager_t *manager);
void system_manager_stop(system_manager_t *manager);
void system_manager_cleanup(system_manager_t *manager);

// 事件处理函数
void system_manager_handle_api_response(system_manager_t *manager, event_t *event);
void system_manager_handle_data_update(system_manager_t *manager, event_t *event);
void system_manager_handle_timer_trigger(system_manager_t *manager, event_t *event);
void system_manager_handle_data_check(system_manager_t *manager, event_t *event);
void system_manager_handle_db_update(system_manager_t *manager, event_t *event);  // 新增数据库更新处理

// 工具函数
void system_manager_setup_api_tasks(system_manager_t *manager);
void system_manager_setup_monitor_tasks(system_manager_t *manager);

// 数据库相关函数
int system_manager_backup_database(system_manager_t *manager, const char *backup_path);
int system_manager_restore_database(system_manager_t *manager, const char *backup_path);

#endif
